package com.zerosky.crm.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zerosky.crm.dao.EmployeeDAO;
import com.zerosky.crm.po.Employee;
import com.zerosky.crm.service.EmployeeService;
import com.zerosky.crm.util.PageInfo;

/**
 * Employee业务逻辑实现类
 * 
 * @author hugy;
 * @since 2017-06-08
 *
 */
@Service(value="employeeService")
public class EmployeeServiceImpl implements EmployeeService {

	@Autowired
	private EmployeeDAO employeeDAO; // Employee数据接口

	/** 验证用户登录信息 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Employee userLogin(Map map) {
		return employeeDAO.userLogin(map);

	}

	/**
	 * Employee列表
	 * 
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Employee> getEmployeeList(Map<String, Object> map) {
		PageInfo<Employee> pageInfo = new PageInfo<Employee>();
		if (map.size() == 0) {
			pageInfo.setList(employeeDAO.getEmployeeList(null));

		} else if ((map.get("branchId") != null || map.get("teamId") != null || map.get("deptId") != null
				|| (map.get("jstreeHeight") != null && map.get("jstreeId") != null)) && map.get("limit") == null) {
			pageInfo.setList(employeeDAO.getEmployeeList(map));
		} else if (map.get("limit") != null && map.get("page") != null) {
			Integer page = (Integer) map.get("page"); // 当前页
			Integer limit = (Integer) map.get("limit"); // 一页显示 几个。
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			pageInfo.setPageCount(employeeDAO.countEmployee(map));
			map.put("pageIndex", (page - 1) * limit);
			map.put("pageSize", limit);
			pageInfo.setList(employeeDAO.getEmployeeList(map));
		}

		return pageInfo;
	}

	/**
	 * 通过主键查找Employee实体
	 * 
	 * @param id
	 *            Employee主键
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Employee getEmployeeById(String id) {
		return employeeDAO.getEmployeeById(id);
	}

	/**
	 * 保存Employee
	 * 
	 * @param Employee
	 *            实体对象, operation 操作类型
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void saveEmployee(Employee employee, String operation) throws Exception {
		if ("新增".equals(operation)) {
			employeeDAO.addEmployee(employee);
		} else if ("修改".equals(operation)) {
			employeeDAO.updateEmployee(employee);
		}

	}

	/**
	 * 批量删除Employee
	 * 
	 * @param idArray
	 *            Employee主键数组
	 * @author hugy;
	 * @since 2017-06-08
	 */
	@Transactional(propagation = Propagation.REQUIRED) // 创建一个事务，如果有则加入事务
	public void deleteEmployeeById(String id) throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			employeeDAO.deleteEmployeeById(id);
		}

	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public Integer countEmployee(Map<String, Object> map) {
		return employeeDAO.countEmployee(map);

	}

	/**
	 * 分页查询当前部门员工所用于的客户数量
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED) // 不开启事务
	public PageInfo<Employee> getEmployeeCustomerNumber(Map<String, Object> map, PageInfo<Employee> page) {
		map.put("pageIndex", (page.getPageNo() - 1) * page.getPageSize());
		map.put("pageSize", page.getPageSize());

		page.setPageCount(employeeDAO.getEmployeeCustomerNumberCount(map));// 总数据量
		page.setList(employeeDAO.getEmployeeCustomerNumber(map));// 数据信息
		return page;

	}

	@Override
	public int checkIsExist(Map<String, Object> map) throws Exception {
		return employeeDAO.checkIsExist(map);
	}

	@Override
	public List<Employee> getEmployeeListAll(Map<String, Object> map) {
		return employeeDAO.getEmployeeList(map);
	}

	@Override
	public int addEmployeeList(List<Employee> list) throws Exception {
		return employeeDAO.addEmployeeList(list);
	}
}
